// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "name": {
    "rules": [
      {
        "required": true,
        "errorMessage": "房间名不能为空"
      },
      {
        "format": "string"
      },
      {
        "minLength": 1,
        "maxLength": 50,
        "errorMessage": "房间名不能为空"
      }
    ],
    "title": "房间名",
    "label": "房间名"
  },
  "address": {
    "rules": [
      {
        "required": true,
        "errorMessage": "地址不能为空"
      },
      {
        "format": "string"
      },
      {
        "minLength": 5,
        "maxLength": 200,
        "errorMessage": "地址太短，请填写详细地址"
      }
    ],
    "title": "地址",
    "label": "地址"
  },
  "area": {
    "rules": [
      {
        "required": true,
        "errorMessage": "面积不能为空"
      },
      {
        "format": "int"
      },
      {
        "minimum": 1,
        "maximum": 1000,
        "errorMessage": "面积必须大于0"
      }
    ],
    "title": "面积",
    "label": "面积"
  },
  "houseType": {
    "rules": [
      {
        "required": true,
        "errorMessage": "户型不能为空"
      },
      {
        "format": "string"
      }
    ],
    "title": "户型",
    "label": "户型"
  },
  "price": {
    "rules": [
      {
        "required": true,
        "errorMessage": "月租金不能为空"
      },
      {
        "format": "int"
      },
      {
        "minimum": 100,
        "maximum": 100000,
        "errorMessage": "月租金不能低于100元"
      }
    ],
    "title": "月租金",
    "label": "月租金"
  },
  "deposit": {
    "rules": [
      {
        "format": "string"
      },
      {
        "range": [
          {
            "value": "押一付一",
            "text": "押一付一"
          },
          {
            "value": "押一付二",
            "text": "押一付二"
          },
          {
            "value": "押一付三",
            "text": "押一付三"
          },
          {
            "value": "押二付三",
            "text": "押二付三"
          },
          {
            "value": "半年付",
            "text": "半年付"
          },
          {
            "value": "年付",
            "text": "年付"
          }
        ],
        "errorMessage": "请选择有效的押金方式"
      }
    ],
    "title": "押金方式",
    "label": "押金方式"
  },
  "floor": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "楼层",
    "label": "楼层"
  },
  "orientation": {
    "rules": [
      {
        "format": "string"
      },
      {
        "range": [
          {
            "value": "东",
            "text": "东"
          },
          {
            "value": "南",
            "text": "南"
          },
          {
            "value": "西",
            "text": "西"
          },
          {
            "value": "北",
            "text": "北"
          },
          {
            "value": "东南",
            "text": "东南"
          },
          {
            "value": "东北",
            "text": "东北"
          },
          {
            "value": "西南",
            "text": "西南"
          },
          {
            "value": "西北",
            "text": "西北"
          }
        ]
      }
    ],
    "title": "朝向",
    "label": "朝向"
  },
  "decoration": {
    "rules": [
      {
        "format": "string"
      },
      {
        "range": [
          {
            "value": "毛坯",
            "text": "毛坯"
          },
          {
            "value": "简装",
            "text": "简装"
          },
          {
            "value": "精装",
            "text": "精装"
          },
          {
            "value": "豪装",
            "text": "豪装"
          }
        ]
      }
    ],
    "title": "装修",
    "label": "装修"
  },
  "facilities": {
    "rules": [
      {
        "format": "array"
      },
      {
        "arrayType": "string"
      }
    ],
    "title": "配套设施",
    "label": "配套设施"
  },
  "images": {
    "rules": [
      {
        "format": "array"
      },
      {
        "arrayType": "file"
      },
      {
        "maxLength": 9,
        "errorMessage": "最多上传9张图片"
      }
    ],
    "title": "房屋图片",
    "label": "房屋图片"
  },
  "status": {
    "rules": [
      {
        "required": true,
        "errorMessage": "房屋状态不能为空"
      },
      {
        "format": "string"
      },
      {
        "range": [
          {
            "value": "已出租",
            "text": "已出租"
          },
          {
            "value": "空置中",
            "text": "空置中"
          }
        ]
      }
    ],
    "title": "房屋状态",
    "defaultValue": "空置中",
    "label": "房屋状态"
  },
  "tenant_id": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "租户ID",
    "label": "租户ID"
  }
}

const enumConverter = {
  "deposit_valuetotext": {
    "押一付一": "押一付一",
    "押一付二": "押一付二",
    "押一付三": "押一付三",
    "押二付三": "押二付三",
    "半年付": "半年付",
    "年付": "年付"
  },
  "orientation_valuetotext": {
    "东": "东",
    "南": "南",
    "西": "西",
    "北": "北",
    "东南": "东南",
    "东北": "东北",
    "西南": "西南",
    "西北": "西北"
  },
  "decoration_valuetotext": {
    "毛坯": "毛坯",
    "简装": "简装",
    "精装": "精装",
    "豪装": "豪装"
  },
  "status_valuetotext": {
    "已出租": "已出租",
    "空置中": "空置中"
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
